package com.heihu577.controller.child;

import com.heihu577.bean.AliyunOSSBean;
import com.heihu577.controller.MyAppController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import lombok.Setter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

@Setter
public class AliyunOSSController {
    private MyAppController myAppController;

    @FXML
    private TextField ACCESS_KEY_ID;

    @FXML
    private TextField ACCESS_KEY_SECRET;

    @FXML
    private TextField ENDPOINT;

    @FXML
    private TextField BUCKET_NAME;

    @FXML
    private Button saveBtn;


    /**
     * 加载时, 初始化 fileInfo
     */
//    private void initFileInfo() {
//        try {
//            fileInfo = FileUtils.getPropertiesFileInputStreamFileOutputStream(configFile);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }

    // 将 properties 文件中的内容, 存入到前端中
    private void loadProperties() {
        ACCESS_KEY_ID.setText(AliyunOSSBean.ACCESS_KEY_ID);
        ACCESS_KEY_SECRET.setText(AliyunOSSBean.ACCESS_KEY_SECRET);
        ENDPOINT.setText(AliyunOSSBean.ENDPOINT);
        BUCKET_NAME.setText(AliyunOSSBean.BUCKET_NAME);
    }

    @FXML
        // 单机保存按钮
    void saveBtnAction(ActionEvent event) throws IOException {
        Properties properties = (Properties) AliyunOSSBean.fileInfo.get("properties");
        try {
            if (!"".equals(ACCESS_KEY_ID.getText()) && !"".equals(ACCESS_KEY_SECRET.getText()) && !"".equals(ENDPOINT.getText()) && !"".equals(BUCKET_NAME.getText())) {
                properties.setProperty("ACCESS_KEY_ID", ACCESS_KEY_ID.getText());
                properties.setProperty("ACCESS_KEY_SECRET", ACCESS_KEY_SECRET.getText());
                properties.setProperty("ENDPOINT", ENDPOINT.getText());
                properties.setProperty("BUCKET_NAME", BUCKET_NAME.getText());
                properties.store(new FileOutputStream((File) AliyunOSSBean.fileInfo.get("file")), null);
                Stage stage = (Stage) ((Button) event.getSource()).getParent().getScene().getWindow();
                myAppController.updateStatus(); // 保存后更新 OSS 配置状态
                // System.out.println(myAppController);
                stage.close(); // 关闭场景
            } else {
                Alert alert = new Alert(Alert.AlertType.ERROR, "请输入完整信息!");
                alert.show();
            }
        } catch (Exception e) {
            Alert alert = new Alert(Alert.AlertType.ERROR, "保存失败!");
            alert.show();
        }
    }


    public void initialize() {
        loadProperties(); // 加载 Properties, 并设置 Properties
        // 不再使用 stage.addEventHandler 调用到 handle 方法的机制, 因为可能控件还没初始化 (为 NULL), 会爆出空指针异常
    }


//    @Override // 窗口加载完毕的事件
//    public void handle(Event event) {
//initFileInfo();
//        loadProperties();
//    }
}
